using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.MainManagers
{
	#region using
	using DevComponents.DotNetBar;
	using AppManagement.Models;
	using AppManagement.Repositories;
	using AppManagement.Helpers;
	using AppManagement.UsersManager;
	using System.Data;
	using System.Drawing;
	using System.IO;
	#endregion

	public partial class FrmCapNhatCHITIETPHIEUYEUCAU : Office2007Form
	{
		public EventHandler Handler { get; set; }
		private DevComponents.DotNetBar.Controls.TextBoxX txtTHIET_BI_ID;

		public FrmCapNhatCHITIETPHIEUYEUCAU()
		{
			InitializeComponent();
			InitHiddenFields();
		}

		public void InitHiddenFields()
		{
			txtTHIET_BI_ID = new DevComponents.DotNetBar.Controls.TextBoxX
			{
				Name = "txtTHIET_BI_ID",
				Text = ""
			};
			txtTHIET_BI_ID.Visible = false;
		}

		private void btnChonTHIETBI_Click(object sender, EventArgs e)
		{
			FrmQuanLyTHIETBI frm = new FrmQuanLyTHIETBI(true);
			frm.Handler += GetTHIETBI;
			frm.ShowDialog();
		}

		public void GetTHIETBI(object sender, EventArgs e)
		{
			var eventType = (TransferSimpleDataEvent)e;
			string[] comp = eventType.Data.Split(new char[] { '#' });
			txtTHIET_BI_ID.Text = comp[0];
			txtTHIETBI.Text = comp[1];
		}


		private bool ValidateInput(ref string errorText)
		{
			if (txtTHIET_BI_ID.Text == "")
			{
				 errorText = "Vui lòng chọn thiết bị";
				return false;
			}
            int n;
            if (!int.TryParse(txtSO_LUONG.Text, out n))
            {
                errorText = "Số lượng yêu cầu không hợp lệ";
                return false;
            }
            if (int.Parse(txtSO_LUONG.Text) <= 0)
            {
                errorText = "Số lượng yêu cầu không hợp lệ";
                return false;
            }
			return true;
		}

		private void btnCapNhat_Click(object sender, EventArgs e)
		{
			var errorText = "";
			if (!ValidateInput(ref errorText))
			{
				MessageBox.Show(errorText, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			TransferDataInfo(this, new TransferSimpleDataEvent(txtTHIET_BI_ID.Text + "#" + txtSO_LUONG.Text));
		}

		public void TransferDataInfo(object sender, TransferSimpleDataEvent e)
		{
			this.Close();
			this.Handler(this, e);
		}

		private void btnThoat_Click(object sender, EventArgs e)
		{
			this.Close();
		}


	}

}
